package com.sam.im.heiqun.uis.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sam.im.heiqun.R;
import com.sam.im.heiqun.uis.adapters.AtHeadAdapter;
import com.yuyh.library.uis.activitys.BaseSwipeBackActivity;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by wangguodong on 2017/8/25.
 */

public class CircleLookWhoActivity extends BaseSwipeBackActivity {
    public static final int SELECT_LLOK_WHO_FOR_RESULT = 1010 ;
    @BindView(R.id.pre_v_back)
    ImageView preVBack;
    @BindView(R.id.pre_tv_title)
    TextView preTvTitle;
    @BindView(R.id.ok)
    TextView ok;
    @BindView(R.id.relative_1)
    RelativeLayout relative_1;
    @BindView(R.id.relative_2)
    RelativeLayout relative_2;
    @BindView(R.id.relative_3)
    RelativeLayout relative_3;
    @BindView(R.id.relative_4)
    RelativeLayout relative_4;
    @BindView(R.id.img_slct_1)
    ImageView img_slct_1;
    @BindView(R.id.img_slct_2)
    ImageView img_slct_2;
    @BindView(R.id.img_slct_3)
    ImageView img_slct_3;
    @BindView(R.id.img_slct_4)
    ImageView img_slct_4;
    @BindView(R.id.recycler_view_sm)
    RecyclerView recycler_view_sm;
    @BindView(R.id.recycler_view_nl)
    RecyclerView recycler_view_nl;

    private int type = 1 ;

    String uids ;
    String uheads ;
    String[] datas ;
    AtHeadAdapter mAtHeadAdapter ;


    public static void startActivity(Activity activity){
        Intent intent = new Intent(activity,CircleLookWhoActivity.class);
        activity.startActivityForResult(intent, SELECT_LLOK_WHO_FOR_RESULT);
    }

    @Override
    protected String getTitleText() {
        return getResources().getString(R.string.who_look);
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        ok.setText(getResources().getString(R.string.success));
        ok.setVisibility(View.VISIBLE);
        type = 1 ;
        closeAll();

    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_circle_look_who;
    }

    @OnClick({R.id.pre_v_back, R.id.ok, R.id.relative_1, R.id.relative_2, R.id.relative_3, R.id.relative_4})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.pre_v_back:
                finish();
                break;
            case R.id.ok:
                Intent intent = new Intent();
                intent.putExtra("type",type);
                intent.putExtra("uids",uids);
                intent.putExtra("uheads",uheads);
                setResult(10,intent);
                finish();
                break;
            case R.id.relative_1:
                type = 1 ;
                closeAll();
                break;
            case R.id.relative_2:
                type = 2 ;
                closeAll();
                break;
            case R.id.relative_3:
                type = 3 ;
                closeAll();
                SelecteGroupFriendActivity.startActivity(CircleLookWhoActivity.this,1,0);
                break;
            case R.id.relative_4:
                type = 4 ;
                closeAll();
                SelecteGroupFriendActivity.startActivity(CircleLookWhoActivity.this,1,0);
                break;
        }
    }

    private void closeAll(){
        img_slct_1.setVisibility(View.GONE);
        img_slct_2.setVisibility(View.GONE);
        img_slct_3.setVisibility(View.GONE);
        img_slct_4.setVisibility(View.GONE);
        switch (type){
            case 1 :
                img_slct_1.setVisibility(View.VISIBLE);
                break;
            case 2 :
                img_slct_2.setVisibility(View.VISIBLE);
                break;
            case 3 :
                img_slct_3.setVisibility(View.VISIBLE);
                break;
            case 4 :
                img_slct_4.setVisibility(View.VISIBLE);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case SelecteGroupFriendActivity.SELECT_FRIEND_IDS_FOR_RESULT :
                if (resultCode == RESULT_OK){
                    uids = data.getStringExtra("uids");
                    uheads = data.getStringExtra("uheads");
                    if (null!=uheads)datas = uheads.split(",");
//                    initAtHead();
                    if (3 == type){
                        initHeadsm();
                    }else if (4 == type){
                        initHeadnl();
                    }
                }
                break;
        }
    }

    private void initHeadsm(){
//        Log.i(TAG, "initAtHead: -----001-----");
        recycler_view_nl.setVisibility(View.GONE);
        if (null==datas || datas.length <=0){
            recycler_view_sm.setVisibility(View.GONE);
        }else {
//            Log.i(TAG, "initAtHead: -----001-----atdatas.length="+datas.length);
            recycler_view_sm.setVisibility(View.VISIBLE);
        }
//        Log.i(TAG, "initAtHead: -----002-----");
        mAtHeadAdapter = new AtHeadAdapter(CircleLookWhoActivity.this,datas);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        linearLayoutManager.setStackFromEnd(false);
        recycler_view_sm.setLayoutManager(linearLayoutManager);
        recycler_view_sm.setAdapter(mAtHeadAdapter);

    }
    private void initHeadnl(){
//        Log.i(TAG, "initAtHead: -----001-----");
        recycler_view_sm.setVisibility(View.GONE);
        if (null==datas || datas.length <=0){
            recycler_view_nl.setVisibility(View.GONE);
        }else {
//            Log.i(TAG, "initAtHead: -----001-----atdatas.length="+datas.length);
            recycler_view_nl.setVisibility(View.VISIBLE);
        }
//        Log.i(TAG, "initAtHead: -----002-----");
        mAtHeadAdapter = new AtHeadAdapter(CircleLookWhoActivity.this,datas);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        linearLayoutManager.setStackFromEnd(false);
        recycler_view_nl.setLayoutManager(linearLayoutManager);
        recycler_view_nl.setAdapter(mAtHeadAdapter);

    }

}
